﻿#region License
// /* **************************************************************
// * Copyright (C) intex Consulting GmbH - All Rights Reserved    *
// *--------------------------------------------------------------*
// * Unauthorized copying of this file, via any medium is         *
// * strictly prohibited                                          *
// *--------------------------------------------------------------*
// * Proprietary and confidential                                 *
// *--------------------------------------------------------------*
// * Dissasembling/modifications strictly prohibited!             *
// ***************************************************************/
#endregion
using System;
using System.Collections;
using ComHlp.ContentClasses.FormDef;

namespace ComHlp.Classes
{
    public static class FormDefCache
    {
        /// <summary> Map cache hashtable </summary>
        private static readonly Hashtable HtFormCache = new Hashtable();

        /// <summary> Check if given map is cached </summary>
        /// <param name="map"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"><paramref name="map"/> is <see langword="null" />.</exception>
        public static bool CheckMapCache(string map)
        {
            if (string.IsNullOrEmpty(map)) throw new ArgumentNullException(nameof(map));

            return HtFormCache.ContainsKey(map);
        }

        /// <summary> Clear complete map cache </summary>
        public static void ClearMapCache()
        {
            HtFormCache.Clear();
        }

        /// <summary> Retrieve cached map </summary>
        /// <param name="map"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"><paramref name="map"/> is <see langword="null" />.</exception>
        public static FormDefMap GetCachedMap(string map)
        {
            if (string.IsNullOrEmpty(map)) throw new ArgumentNullException(nameof(map));

            if (!HtFormCache.ContainsKey(map))
                return null;

            FormDefMap formDefMap = (FormDefMap) HtFormCache[map];
            return formDefMap.Clone();
        }

        /// <summary> Add map to cache </summary>
        /// <param name="frmDef"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"><paramref name="frmDef"/> is <see langword="null" />.</exception>
        public static void AddMapToCached(FormDefMap frmDef)
        {
            if(frmDef.Header == null) throw new ArgumentNullException(nameof(frmDef));

            HtFormCache[frmDef.Header.Name] = frmDef;
        }
    }
}